#include <bits/stdc++.h>
using namespace std;
int main() {
    long long n = 0, q = 0;
    cin >> n >> q;
    vector<pair<string, int>> res(30010, make_pair("",0));
    // 还有哪些像set一样的容器是方便查找的
    set<string> user;
    string quote = "";
    for(int i = 0; i < q; ++i) {
        cin >> quote;
        user.insert(quote);
        quote.clear();
    }
    string product = "";
    int m = 0;
    for(long long i = 0; i < n; ++i) {
        cin >> product;
        cin >> m;
        res[i].first = product;
        for(long long j = 0; j < m; ++j) {
            string description = "";
            cin >> description;
            if(user.find(description)!=user.end()) {
                res[i].second++;
            }
        }
        product.clear();
    }
    sort(res.begin(), res.end(), [&](pair<string,int> &a, pair<string,int> &b) {
        if(a.second == b.second) return false;
        if(a.second > b.second) return true;
        else return false;
    });
    for(const pair<string,int>& pa : res) {
        cout << pa.first << endl;
    }
}